package cate.game.attr;

import cate.common.table.d.GDAttr;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

@NoteClass(value = "英雄属性成长")
public class HeroAttrGrow {

    @NoteField(value = "原始数据Map<属性枚举(Integer), 属性成长值(Double)>")
    public Map<Integer, Double> raw = new HashMap<>();

    public HeroAttrGrow() {}

    public HeroAttrGrow(String code) {
        parse(code);
    }

    public void parse(String code) {
        if (StringUtils.isBlank(code)) {
            return;
        }
        String[] kvs = code.split("#");
        for (String kv : kvs) {
            if (StringUtils.isBlank(kv)) {
                continue;
            }
            String[] kvArr = kv.split(":");
            if (kvArr.length >= 2) {
                int k = Integer.parseInt(kvArr[0]);
                double v = Double.parseDouble(kvArr[1]);
                raw.put(k, v);
            }
        }
    }

    private String getKeyName(Integer k) {
        return GDAttr.getKeyName(k);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("【");
        raw.forEach(
                (k, v) -> {
                    sb.append(getKeyName(k));
                    sb.append(":");
                    sb.append(v);
                    sb.append(", ");
                });
        sb.append("】");
        return sb.toString();
    }
}
